package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.sevrice.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired(required = false)
    public DishService dishService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<DishDto> page = dishService.findPage(pageParam);
        return  R.success(page);
    }

    /**
     * 添加菜品 //这个要需要增加口味表的数据
     * @param dishDto
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody DishDto dishDto){
        int row = dishService.addDish(dishDto);
        if (row>0){
            return R.success("添加菜品成功！");
        }
        return R.error("添加菜品失败~！");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        int row = dishService.deleteDish(ids);
        if (row>0){
            return R.success("删除菜品成功！");
        }
        return R.error("删除菜品失败~！");
    }

    /**
     * 修改菜品的售卖状态
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        System.out.println(("状态值："+status+"所需要更新id有:" + ids));
        int row = dishService.updateStatus(status, ids);
        if (row>0){
            return R.success("修改菜品售卖状态成功！");
        }
        return R.error("修改菜品售卖状态失败~！");
    }

    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R updateDish(@RequestBody DishDto dishDto){
        int row = dishService.updateDish(dishDto);
        //响应结果
        if (row>0){
            return R.success("更新菜品成功！");
        }
        return R.error("更新菜品失败！");
    }

    /**
     * 根据菜品分类 查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId, Integer status){
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,status);
        return R.success(dishList);
    }


}
